#!/bin/bash

function get_begin_time()
{
    starttime=`date +'%Y-%m-%d %H:%M:%S'`
    start_timestamp=$(date --date="$starttime" +%s);
}

function get_execution_duration()
{
    endtime=`date +'%Y-%m-%d %H:%M:%S'`
    end_timestamp=$(date --date="$endtime" +%s);
    executionDurationTime=$((end_timestamp-start_timestamp))
}

function do_aicore_test()
{
    get_begin_time
    boolTime=0

    # 捕捉 Ctrl + C (SIGINT) 信号，退出循环
    trap "echo 'Caught Ctrl + C, exiting...'; exit" SIGINT

    while (( ${boolTime}>=0 ))
    do
        echo "Y" | ascend-dmi -f  # 自动输入 "Y"
        npu-smi info
        # rm /var/log/ascend-dmi/*
        # rm /var/log/nputools*
        # rm /var/log/messages
        get_execution_duration
        boolTime=$[${exeTimeSecond}-${executionDurationTime}]
    done
}

main()
{
    source ~/.bashrc
    exeTimeSecond=600000000000
    if [ ! -z $1 ]; then
        exeTimeSecond=$1
    fi
    if [[ $1 == *h ]]; then
        t=$1
        exeTimeHours=${t%h*}
        exeTimeSecond=$(( exeTimeHours*60*60 ))
    fi
    echo "Execution duration:${exeTimeSecond}(s)"
    do_aicore_test $exeTimeSecond
    echo "Done"
}

main $*
